﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.Entity;
using IRepository;
using RepositoryImp;
using OA.DataBase;

namespace UnitWorkContext
{
    public class UnitOfWork : IUnitOfWork
    {
        private readonly OAContext _context;
        private IUserRepository userrepository;
        private IModuleRepository modulerepository;
        private ISysActionRepository sysactionrepository;

        private bool disposed = false;
        protected virtual void Dispose(bool disposing)
        {
            if (!this.disposed)
            {
                if (disposing)
                {
                    _context.Dispose();
                }
            }
            this.disposed = true;
        }
        public void Dispose()
        {
            Dispose(true);
            GC.SuppressFinalize(this);
        }
        ~UnitOfWork()
        {
            Dispose(false);
        }

        public UnitOfWork(OAContext context)
        {
            _context = context;
        }

        public void Save()
        {
            _context.SaveChanges();
        }
        public IUserRepository UserRepository
        {
            get
            {
                if (modulerepository == null)
                    userrepository = new UserRepository(_context);
                return userrepository;
            }
        }


        public IModuleRepository ModuleRepository
        {
            get
            {
                if (modulerepository == null)
                    modulerepository = new ModuleRepository(_context);
                return modulerepository;
            }
        }


        public ISysActionRepository SysActionRepository
        {
            get
            {
                 if (sysactionrepository == null)
                    sysactionrepository = new SysActionRepository(_context);
                return sysactionrepository;
            }
            
        }
    }

}
